package com.dhcc.bpm.modules.metadata.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.common.annotation.Log;
import com.dhcc.bpm.common.enums.BusinessType;
import com.dhcc.bpm.modules.metadata.entity.ResMetadata;
import com.dhcc.bpm.modules.metadata.service.IResMetadataService;
import com.dhcc.bpm.modules.metadata.vo.ResMetadataVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName ResMetadataController
 * @Description 响应元数据关联表 控制器
 * @Author daina
 * @Date 2020/2/21 14:16
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 */
@RestController
@AllArgsConstructor
@RequestMapping("bpm-metadata/resmetadata")
@Api(value = "响应元数据关联表", tags = "响应元数据关联表接口")
public class ResMetadataController extends BladeController {

    private IResMetadataService resMetadataService;

    /**
     * @Description 批量取消响应参数信息
     * @Author daina
     * @Date 15:26 2020/3/18
     * @param opcApiIdRes
     * @param ids
     * @return org.springblade.core.tool.api.R
     **/
    @PostMapping("/deleteResMetadata")
    @ApiOperationSupport(order = 7)
    @ApiLog("批量取消响应参数信息")
    @ApiOperation(value = "逻辑删除", notes = "传入ids")
    @Log(title = "批量取消响应参数信息", businessType = BusinessType.DELETE)
    public R deleteResMetadata(@ApiParam(name = "opcApiIdRes", value = "网关id") @RequestParam Long opcApiIdRes, @ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        int apis = resMetadataService.deleteResMetadata(opcApiIdRes, ids);
        if (apis > 0) {
            return R.status(true);
        } else {
            return R.status(false);
        }
    }

    /**
     * @Description 响应参数批量添加元数据
     * @Author daina
     * @Date 10:48 2020/2/22
     * @param opcApiIdRes
     * @param ids
     * @return org.springblade.core.tool.api.R
     **/
    @PostMapping("/addResMetadata")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "添加", notes = "opcApiIdRes,ids")
    @ApiLog("响应参数批量添加元数据")
    @Log(title = "接口响应参数批量添加元数据", businessType = BusinessType.INSERT)
    public R addResMetadata(@ApiParam(name = "opcApiIdRes", value = "接口id") @RequestParam Long opcApiIdRes, @ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        List<Long> metaIds = Func.toLongList(ids);
        ArrayList<ResMetadata> list = new ArrayList<>();
        metaIds.forEach(metaId -> {
            ResMetadata res = new ResMetadata();
            res.setOpcApiIdRes(opcApiIdRes);
            res.setOpcMetadataId(metaId);
            list.add(res);
        });
        return R.status(resMetadataService.saveBatch(list));
    }

    /**
     * @Description 详情
     * @Author daina
     * @Date 10:48 2020/2/22
     * @param resMetadata
     * @return org.springblade.core.tool.api.R<com.dhcc.bpm.modules.metadata.entity.ResMetadata>
     **/
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "传入resMetadata")
    @ApiLog("详情")
    @Log(title = "详情", businessType = BusinessType.SELECT)
    public R<ResMetadata> detail(ResMetadata resMetadata) {
        ResMetadata detail = resMetadataService.getOne(Condition.getQueryWrapper(resMetadata));
        return R.data(detail);
    }

    /**
     * @Description 分页 响应元数据关联表
     * @Author daina
     * @Date 10:48 2020/2/22
     * @param resMetadata
     * @param query
     * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.metadata.entity.ResMetadata>>
     **/
    @GetMapping("/list")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "分页", notes = "传入resMetadata")
    @ApiLog("查询响应元数据关联表")
    @Log(title = "查询响应元数据关联表", businessType = BusinessType.SELECT)
    public R<IPage<ResMetadata>> list(ResMetadata resMetadata, Query query) {
        IPage<ResMetadata> pages = resMetadataService.page(Condition.getPage(query), Condition.getQueryWrapper(resMetadata));
        return R.data(pages);
    }

    /**
     * @Description 分页 响应元数据关联表
     * @Author daina
     * @Date 10:49 2020/2/22
     * @param resMetadata
     * @param query
     * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.metadata.vo.ResMetadataVO>>
     **/
    @GetMapping("/page")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "分页", notes = "传入resMetadata")
    @ApiLog("查询")
    @Log(title = "查询", businessType = BusinessType.SELECT)
    public R<IPage<ResMetadataVO>> page(ResMetadataVO resMetadata, Query query) {
        IPage<ResMetadataVO> pages = resMetadataService.selectResMetadataPage(Condition.getPage(query), resMetadata);
        return R.data(pages);
    }

    /**
     * @Description 新增 响应元数据关联表
     * @Author daina
     * @Date 10:49 2020/2/22
     * @param resMetadata
     * @return org.springblade.core.tool.api.R
     **/
    @PostMapping("/save")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "新增", notes = "传入resMetadata")
    @ApiLog("新增")
    @Log(title = "新增", businessType = BusinessType.INSERT)
    public R save(@Valid @RequestBody ResMetadata resMetadata) {
        return R.status(resMetadataService.save(resMetadata));
    }

    /**
     * @Description 修改 响应元数据关联表
     * @Author daina
     * @Date 10:49 2020/2/22
     * @param resMetadata
     * @return org.springblade.core.tool.api.R
     **/
    @PostMapping("/update")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改", notes = "传入resMetadata")
    @ApiLog("修改")
    @Log(title = "修改", businessType = BusinessType.UPDATE)
    public R update(@Valid @RequestBody ResMetadata resMetadata) {
        return R.status(resMetadataService.updateById(resMetadata));
    }

    /**
     * @Description 新增或修改 响应元数据关联表
     * @Author daina
     * @Date 10:49 2020/2/22
     * @param resMetadata
     * @return org.springblade.core.tool.api.R
     **/
    @PostMapping("/submit")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "新增或修改", notes = "传入resMetadata")
    @ApiLog("修改")
    @Log(title = "修改", businessType = BusinessType.UPDATE)
    public R submit(@Valid @RequestBody ResMetadata resMetadata) {
        return R.status(resMetadataService.saveOrUpdate(resMetadata));
    }


    /**
     * @Description 删除 响应元数据关联表
     * @Author daina
     * @Date 10:49 2020/2/22
     * @param ids
     * @return org.springblade.core.tool.api.R
     **/
    @PostMapping("/remove")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "删除", notes = "传入ids")
    @ApiLog("删除")
    @Log(title = "删除", businessType = BusinessType.DELETE)
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        return R.status(resMetadataService.removeByIds(Func.toLongList(ids)));
    }


}
